<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<script src="../plugins/ueditor/ueditor.config.js" type="text/javascript"></script>
<script src="../plugins/ueditor/ueditor.all.min.js" type="text/javascript"></script>
<script src="../plugins/ueditor/lang/zh-cn/zh-cn.js" type="text/javascript"></script>
<div id="formbody" class="formbody">
    <div class="itab">
       <ul>
          	<?php foreach ($category as $c){?>
            <li><a href="#tab<?php echo $c['id']?>"><?php echo $c['title']?></a></li>
            <?php }?>
       </ul>
    </div>
    <div id="tab_content" class="tabson" style="display:block;"></div>
</div>

<!-- 模板 -->
<script id="list" type="text/html">
    <table class="tablelist">
        <thead>
        <tr>
        <th style="width:8%">编号</th>
        <th>名称</th>
        <th style="width:30%">英文名称</th>
        <th style="width:8%">排序</th>
        <th style="width:8%">启用</th>
        <th style="width:15%">操作</th>
        </tr>
        </thead>
        <tbody>
        <%for(var i = 0; i < list.length; i ++) {%>
            <tr <%=#(i%2 ==0) ?'':'class="odd"'%> data-id="<%=list[i].id%>">
            <td><%=list[i].id%></td>
            <td><%=list[i].title%></td>
            <td><%=list[i].subtitle%></td>
            <td><%=Number(list[i].sort)%></td>
            <td><%=#list[i].enable?'是':'<i>否</i>'%></td>
            <td>
                <a class="tablelink act_edit">编辑</a>
                <a class="tablelink act_delete">删除</a>
                <?php if($is_bilingual){?>
                <a class="tablelink act_edit_en">英文版</a>
                <?php }?>
            </td>
            </tr>
        <%}%>        
        </tbody>
    </table>  
    <div class="pagin">
        <div class="message">共<i class="blue"><%=record_count%></i>条记录，当前显示第&nbsp;<i class="blue"><%=page_num%>/<%=page_count%></i>&nbsp;页</div>
        <div class="paginList" id="page_bar"></div>
    </div>
    <div class="clearfloat"></div>
</script>
                
<script id="edit" type="text/html">
<form action="<?php echo site_url('ajax/content_handle/modify');?>" method="POST">  
<div class="formtitle"><span><%=id?'编辑':'添加'%>信息</span></div> 
<input name="id" type="hidden" value="<%=id?id:0%>" /> 
<input name="cid" type="hidden" value="<%=tab.replace('tab','')%>" />
<ul class="forminfo">
    <li><label>名称<b>*</b></label><input name="title" value="<%=title%>" type="text" class="dfinput" /></li>
    <li><label>英文名称</label><input name="subtitle" value="<%=subtitle%>" type="text" class="dfinput" /></li>
    <li><label>内容</label><script id="editor_content" name="content" type="text/plain" class="editor"><%=#content%></<%='script'%>></li>
    <li><label>排序</label><input name="sort" value="<%=Number(sort)%>" type="text" class="dfinput" /><i>数值大的在前，数值小的在后</i></li>
    <li><label>SEO标题</label><input name="head_title" value="<%=head_title%>" style="width:480px" type="text" class="dfinput" /><i>title标签</i></li>
    <li><label>SEO关键词</label><input name="head_keywords" value="<%=head_keywords%>" style="width:480px" type="text" class="dfinput" /><i>对应keywords</i></li>
    <li><label>SEO描述</label><textarea name="head_description" style="height:80px" class="textinput"><%=head_description%></textarea><i>对应description</i></li>
    <li><label>是否启用</label><cite><input id="enable_yes" name="enable" type="radio" value="1" <%=enable?'checked="checked"':''%>/><label for="enable_yes">是</label><input id="enable_no" name="enable" type="radio" value="0"  <%=enable?'':'checked="checked"'%>/><label for="enable_no">否</label></cite></li>
    <li><label>&nbsp;</label><input type="submit" class="btn" value="确认保存"/><input type="button" class="btn btn_cancle" value="取消"/></li>
</ul>
</form>
</script>

<script id="edit_en" type="text/html">
<form action="<?php echo site_url('ajax/content_handle/modify');?>" method="POST">  
<div class="formtitle"><span><%=id?'编辑':'添加'%>信息(英文版)</span></div> 
<input name="id" type="hidden" value="<%=id?id:0%>" /> 
<input name="cid" type="hidden" value="<%=tab.replace('tab','')%>" />
<ul class="forminfo">
    <li><label>名称</label><input name="subtitle" value="<%=subtitle%>" type="text" class="dfinput" /></li>
    <li><label>内容</label><script id="editor_content" name="en_content" type="text/plain" class="editor"><%=#en_content%></<%='script'%>></li>
    <li><label>SEO标题</label><input name="en_head_title" value="<%=en_head_title%>" style="width:480px" type="text" class="dfinput" /><i>title标签</i></li>
    <li><label>SEO关键词</label><input name="en_head_keywords" value="<%=en_head_keywords%>" style="width:480px" type="text" class="dfinput" /><i>对应keywords</i></li>
    <li><label>SEO描述</label><textarea name="en_head_description" style="height:80px" class="textinput"><%=en_head_description%></textarea><i>对应description</i></li>
    <li><label>&nbsp;</label><input type="submit" class="btn" value="确认保存"/><input type="button" class="btn btn_cancle" value="取消"/></li>
</ul>
</form>
</script>

<!-- 脚本 -->
<script src="js/template-helper.js" type="text/javascript"></script>
<script type="text/javascript">

function get_page(){
	$('.btn_add').show();
	$('.btn_back').hide();
	$.getJSON(
        '<?php echo site_url('ajax/content_handle/get_info_list');?>',
        {
              begin: (current_status.page_num - 1) * current_status.page_size,
              length: current_status.page_size,
              cid: current_status.tab.replace('tab','')
          },
          function(rs){
              if(rs.err===0){
                current_status.page_count = Math.ceil(rs.data.record_count/current_status.page_size);
                //呈现内容
                $('#tab_content').html(template('list', $.extend({},current_status,rs.data)));
                //显示分页
                laypage({
                    cont: 'page_bar', //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="page1"></div>
                    pages: current_status.page_count, //通过后台拿到的总页数
                    curr: current_status.page_num, //当前页
                    jump: function(obj, is_self){ //触发分页后的回调
                        if(!is_self){ //点击跳页触发函数自身，并传递当前页：obj.curr
                            current_status.page_num = obj.curr;
                            get_page();
                        }
                    }
                });
              }else if(rs.err===4){
                  //数据实际页数减少，但前端没有刷新，仍旧请求不存在的页码，返回的是“列表为空”的错误
                  current_status.page_num --;
                  get_page();
              }else{
                  layer.msg(rs.msg, {icon:2,time:2000});
              }            
        }
    );
};
var current_status = { page_num:1, page_size:20, page_count:1 };
current_status.tab = window.location.hash?window.location.hash.substring(1).replace('t','tab'):0;
$("#formbody .itab ul").idTabs({
	start:current_status.tab,
	click:function(tab){
		current_status.tab = tab.substring(1);
		window.location.hash = current_status.tab.replace('tab','t');
		current_status.page_num = 1;
		get_page();		
		return true;
	}
});
//添加
$('.btn_add').on('click', function(){
	$(this).hide();
	$('.btn_back').show();
	$('#tab_content').html(template('edit', $.extend({sort:0,enable:true},current_status)));
	if(window.ueditor){
		window.ueditor.destroy();
	}
	window.ueditor = UE.getEditor('editor_content',{
		UEDITOR_HOME_URL: '<?php echo base_url();?>public/plugins/ueditor/',
		serverUrl: '<?php echo site_url('ajax/ueditor_handle/index');?>',
		iframeBasePath:'<?php echo base_url();?>public/admin/'
    });
});
//返回列表
$('.btn_back').on('click', function(){
	get_page();
});
//取消
$('#formbody').on('click', '.btn_cancle', function(){
	get_page();
});
//删除
$('#formbody').on('click', '.act_delete',function(){
    var id = $(this).parent().parent().attr('data-id');
    $.post(
            '<?php echo site_url('ajax/content_handle/modify');?>',
            {
                  id:id,
                  deleted:1
            },
            function(rs){
                if(rs.err===0){
                    layer.msg('删除成功！', {icon:1,time:2000});    
                    get_page();
                }else{
                    layer.msg(rs.msg, {icon:2,time:2000});
                }
            },
            'json'
    );
});
//编辑
$('#formbody').on('click', '.act_edit,.act_edit_en', function(){
	var id = $(this).parent().parent().attr('data-id');
	var btn_text = $(this).html(); 
	$('.btn_add').hide();
	$('.btn_back').show();
	$.getJSON(
            '<?php echo site_url('ajax/content_handle/get');?>',
            {
                id:id
            },
            function(rs){
                if(rs.err===0){
                	$('#tab_content').html(template(btn_text=='英文版'?'edit_en':'edit', $.extend({},current_status,rs.data)));
	            	if(window.ueditor){
                		window.ueditor.destroy();
	            	}
                	window.ueditor = UE.getEditor('editor_content',{
                		UEDITOR_HOME_URL: '<?php echo base_url();?>public/plugins/ueditor/',
                		serverUrl: '<?php echo site_url('ajax/ueditor_handle/index');?>',
                		iframeBasePath:'<?php echo base_url();?>public/admin/'
    	            });
                }else{
                    layer.msg(rs.msg, {icon:2,time:2000});
                }
            }
    );	
});
//保存
$('#formbody').on('submit','form', function(e) {
    e.preventDefault();
	var this_btn = $(this);
    if(!this_btn.attr('disabled')){
    	this_btn.attr('disabled',true);
    }else{
        return;
    }
    $(this).ajaxSubmit({
    	dataType:'json',
    	success:function(rs){
    		if(rs.err===0){
                layer.msg('保存成功！', {icon:1,time:2000}, function(){
                	get_page();
                });
            }else{
                layer.msg(rs.msg, {icon:2,time:2000}, function(){
                	this_btn.attr('disabled',false);
                });
            }
		}
    });
});
</script>
